<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View, Session};

use app\common\model\{Admin, Menu, Role};

class IndexController extends BaseController
{
    private $token;
    public function __construct(Admin $adminModel)
    {
        parent::initialize();
        $this->adminModel = $adminModel;
        $this->token = Session::get('token');
    }

    public function index()
    {
        $menuModel = new Menu();
        $roleModel = new Role();
        $a = Session::get('token');

        $where = ['pid' => 0, 'is_show' => 1];
        $menu = $menuModel->getList($where, 0)['data'];

        foreach ($menu as $key => $value) {
            $menu[$key]['list'] = $menuModel->getList(['pid' => $value['id'], 'is_show' => 1], 0)['data'];
        }

        if($this->token['role_id'] > 0){
            $role_info = $roleModel->getFind(['id' => $this->token['role_id']])['data'];

            $role_arr = explode(',', $role_info->getData('menu'));
            array_pop($role_arr);
        }else{
            $role_arr = [];
        }

        View::assign('menu', $menu);
        View::assign('role', $role_arr);
        View::assign('admin', $this->token);
        return view();
    }

    public function home()
    {
        View::assign('admin', $this->token);
        return view();
    }
}
